home *** CD-ROM | disk | FTP | other *** search
- /*________________________________________________________
-
- File: Confidential.c
-
- C code for a printing extension that stamps a message
- on each page spooled.
-
- Dave Hersey
- Apple Developer Technical Support
-
- 2/01/93 - dmh - Completely rewrote for the a5 seed.
- 4/26/93 - dmh - Renamed files for b1 seed.
- 4/27/93 - dmh - Updated NewSpoolPage override to work
- w/o CopyDeepToShape.
- 9/05/93 - dmh - Updated for b2.
- - removed work-around for a 1.0a5
- panel bug. That should have been
- removed in the b1 version… oops.
- - Fixed minor bug with panel's editText
- fields' highlighting.
- - Switched to Exception.h assertion stuff
- for error checking.
- 12/18/93 - dmh - Updated for b3.
- 3/22/94 - dmh - Updated for b4.
-
- (Note: all functions are in the Mark menu.)
-
- __________________________________________________________*/
-
- #include "Confidential.h"
-
-
- /*******************************************************************
- __Startup__ contains our jump table to the overrides.
-
- ********************************************************************/
- #if defined(__MWERKS__)
- asm void __Startup__(void) ;
- asm void __Startup__(void)
- {
- DC.L 0 // GX needs this
-
- JMP NewJobPrintDialog // (offset = 4)
- JMP NewHandlePanelEvent // (offset = 8)
- JMP NewSpoolPage // (offset = 12)
-
- RTS // this is needed so __Startup__ symbol works
- }
- #endif
-
-
- /*******************************************************************
- NewJobPrintDialog is our override for GXJobPrintDialog. All we
- do is set up our panel and then forward the message.
-
- ********************************************************************/
-
- OSErr NewJobPrintDialog(gxDialogResult *dlogResult)
- {
- OSErr err;
-
- err = SetUpPrintPanel();
-
- if (!err)
- err = Forward_GXJobPrintDialog(dlogResult);
-
- return err;
- }
-
-
- /*******************************************************************
- NewHandlePanelEvent is our override for GXHandlePanelEvent. If
- the event is one of ours, we handle it. Otherwise, we just
- forward it down the chain.
-
- ********************************************************************/
-
- OSErr NewHandlePanelEvent(gxPanelInfoRecord *panelInfo)
- {
- GrafPtr oldPort;
- DialogPtr pDlg;
-
- // Get a pointer to the dialog, save our current grafPort,
- // and set us to the dialog's port.
-
- pDlg = panelInfo->pDlg;
- GetPort(&oldPort);
- SetPort(pDlg);
-
- switch (panelInfo->panelEvt)
- {
-
- // If our panel is activating/deactivating or if the focus (which
- // section of the dialog is active) has changed we need to activate
- // our editText fields appropriately.
-
- case gxPanelActivateEvt: // The panel is becoming active.
- case gxPanelDeactivateEvt: // The panel is becoming inactive.
- case gxPanelIconFocusEvt: // The user is moving to the icon list.
- case gxPanelPanelFocusEvt: // The user is moving to the panel.
-
- if ((((DialogPeek) pDlg)->editField +1) == (panelInfo->itemCount +d_message) ||
- (((DialogPeek) pDlg)->editField +1) == (panelInfo->itemCount +d_fontSize))
- {
- if (panelInfo->panelEvt == gxPanelPanelFocusEvt)
- TEActivate(((DialogPeek) pDlg)->textH);
- else
- TEDeactivate(((DialogPeek) pDlg)->textH);
- }
- break;
- }
-
- // Restore the original port as we leave.
-
- SetPort(oldPort);
- return noErr;
- }
-
-
- /*******************************************************************
- NewSpoolPage is our override for the GXSpoolPage message. We
- check to see if we're enabled, and if so, add the desired text
- shape to the page before forwarding.
-
- ********************************************************************/
-
- OSErr NewSpoolPage(gxSpoolFile spFile, gxFormat aFormat, gxShape pgShape)
- {
- OSErr err;
- long idx, numParts;
- StampCollection stampConfig;
- Boolean addedStamp = false;
- gxShape msgShapeRef, shapeRef = nil;
-
- // Load our stamp collection item.
-
- err = GetStamp(&stampConfig);
- require((err != collectionItemNotFoundErr), NoStampSetUp);
- nrequire(err, CouldNotGetStamp);
-
-
- // If the stamp is turned on, add the text to the page shape, and keep a
- // reference to it so we make sure we remove the correct picture item on
- // exit. We must return the picture as we received it.
-
- if (stampConfig.stampEnabled)
- {
- err = AddStampToPage(&stampConfig, pgShape, aFormat);
- nrequire(err, CouldNotAddStamp);
-
- addedStamp = true;
- GXGetPictureParts(pgShape, 1, 1, &msgShapeRef, nil, nil, nil);
- }
-
-
- // Forward the GXSpoolPage message on to other handlers.
-
- NoStampSetUp:
-
- err = Forward_GXSpoolPage(spFile, aFormat, pgShape);
- require(addedStamp, DidNotAddStamp);
-
-
- // If we added a stamp, we now have to remove it from the original picture.
- // Our stamp should be the first shape in the picture, but it's possible that
- // an incorrectly written printing extension below us has changed the picture
- // and not undone its deed. To be extra sure that we remove the correct item,
- // we compare each shape reference in the picture with the reference of our
- // stamp shape. If/when we find the stamp, we remove the shape from the
- // picture. Finally, if an error didn't occur during forwarding, we make one
- // last stab at finding a problem to report.
-
- numParts = GXGetPicture(pgShape, nil, nil, nil, nil);
-
- for (idx = 1; (shapeRef != msgShapeRef) && (idx <= numParts); idx++)
- {
- GXGetPictureParts(pgShape, idx, 1, &shapeRef, nil, nil, nil);
-
- if (shapeRef == msgShapeRef)
- GXSetPictureParts(pgShape, idx, 1, 0, nil, nil, nil, nil);
- }
-
- if (!err)
- err = (OSErr) GXGetGraphicsError(nil);
-
- CouldNotGetStamp:
- CouldNotAddStamp:
- DidNotAddStamp:
-
- return err;
- }
-
-
- /*******************************************************************
- AddStampToPage adds our text stamp to the page shape passed.
-
- ********************************************************************/
-
- OSErr AddStampToPage(StampCollection *stampConfig, gxShape pgShape, gxFormat aFormat)
- {
- gxGraphicsError aGrphErr;
- gxRectangle pBounds;
- gxShape msgShape;
- gxPoint textCtr;
- Fixed cx, cy;
- gxColor msgColor;
- gxFormat curFormat;
-
- // Create a new text shape to add to the page, based on what's in our
- // collection item.
-
- msgShape = GXNewText((long) stampConfig->message[0],
- (const unsigned char *) &stampConfig->message[1], nil);
-
-
- // If no errors, set the shape's colorspace to gxGraySpace and color it
- // light gray. Set the font's size as appropriate.
-
-
- aGrphErr = GXGetGraphicsError(nil);
- nrequire(aGrphErr, CouldNotCreateText);
-
- msgColor.space = gxGraySpace;
- msgColor.profile = nil;
- msgColor.element.gray = 190 <<8;
- GXSetShapeColor(msgShape, &msgColor);
- GXSetShapeTextSize(msgShape, ff(stampConfig->fontSize));
-
-
- // If no errors, get the dimensions of the format's page size. Remember
- // that the format passed to us may be nil, so use the job's format in
- // that case.
-
- GXGetGraphicsError(&aGrphErr);
- nrequire(aGrphErr, CouldNotSetUpText);
-
- curFormat = (aFormat != nil)? aFormat: GXGetJobFormat(GXGetJob(), 1);
- GXGetFormatDimensions(curFormat, &pBounds, nil);
-
-
- // If no errors, get the page and the text shape's center and move the text
- // so that it's centered on the page.
-
- aGrphErr = (gxGraphicsError) GXGetJobError(GXGetJob());
- nrequire(aGrphErr, CouldNotAccessFormat);
-
- cx = pBounds.left + (pBounds.right - pBounds.left) >>1;
- cy = pBounds.top + (pBounds.bottom - pBounds.top) >>1;
- GXGetShapeCenter(msgShape, 0, &textCtr);
- GXMoveShapeTo(msgShape, cx -textCtr.x, cy -textCtr.y);
-
-
- // Rotate the stamp about its (new) center point 45° and add it to the
- // beginning of the picture so that it draws behind the printed
- // information. Note that you can call GXSetPictureParts(pgShape, 0, 0, 1…)
- // to place the text *on top* of everything on the page.
-
- GXGetShapeCenter(msgShape, 0, &textCtr);
- GXSetShapeAttributes(msgShape,
- GXGetShapeAttributes(msgShape) | gxMapTransformShape);
-
- GXRotateShape(msgShape, ff(45), textCtr.x, textCtr.y);
-
- GXSetPictureParts(pgShape, 1, 0, 1, &msgShape, nil, nil, nil);
- GXGetGraphicsError(&aGrphErr);
-
-
- // Dispose of the text shape on the way out.
-
- CouldNotAccessFormat:
- CouldNotSetUpText:
-
- GXDisposeShape(msgShape);
-
- CouldNotCreateText:
-
- return (OSErr) aGrphErr;
- }
-
-
- /*******************************************************************
- SetUpPrintPanel sets up our print panel, adding a default
- StampCollection item to the job collection. This collection
- item has the values we'll use to set up our panel's controls.
-
- ********************************************************************/
-
- OSErr SetUpPrintPanel()
- {
- OSErr err;
- gxPanelSetupRecord panelSetupRec;
- StampCollection stampConfig;
- Boolean newItem = false;
-
- // Try to find our collection item.
-
- err = GetCollectionItem(GXGetJobCollection(GXGetJob()),
- kStampCollectionType,
- gxPrintingTagID,
- nil,
- &stampConfig);
-
-
- // If we don't have an item in the job collection yet, store our default
- // settings in it.
-
- if (err == collectionItemNotFoundErr)
- {
- err = GetDefaultSettings(&stampConfig);
-
- if (!err)
- err = AddCollectionItem(GXGetJobCollection(GXGetJob()),
- kStampCollectionType,
- gxPrintingTagID,
- sizeof(StampCollection),
- &stampConfig);
-
- nrequire(err, HaveCollectionMgrError);
- }
-
-
- // Now, do the actual panel set up.
-
- panelSetupRec.panelResId = r_stampPanel; // which panel resource?
- panelSetupRec.resourceRefNum = GXGetMessageHandlerResFile(); // where is it?
- panelSetupRec.refCon = 0; // we don't use this.
- panelSetupRec.panelKind = gxExtensionPanel; // This is an extension panel.
-
- err = GXSetupDialogPanel(&panelSetupRec);
-
-
- HaveCollectionMgrError:
-
- return err;
- }
-
-
- /*******************************************************************
- GetJobCollectionItem is a generic routine that retrieves a
- collection item from the job collection.
-
- ********************************************************************/
-
- OSErr GetJobCollectionItem(void *collectItem, long *collectSize,
- OSType collectType, short collectID)
- {
- return GetCollectionItem(GXGetJobCollection(GXGetJob()),
- collectType,
- collectID,
- collectSize,
- collectItem);
- }
-
-
- /*******************************************************************
- GetDefaultSettings returns a StampCollection item with default
- settings from our extension's resource file.
-
- ********************************************************************/
-
- OSErr GetDefaultSettings(StampCollection *stampConfig)
- {
- OSErr err;
- short oldResFile;
- Str255 fontSizeStr;
-
- // Save the current resource file and get our extension's.
-
- oldResFile = CurResFile();
- UseResFile(GXGetMessageHandlerResFile());
-
-
- // Now set up the default settings in the structure passed.
-
- stampConfig->stampEnabled = false;
- GetIndString(stampConfig->message, r_str, r_defaultStrIdx);
- err = ResError();
- nrequire(err, CouldNotLoadString);
-
- GetIndString(fontSizeStr, r_str, r_defaultFSizeIdx);
- StringToNum(fontSizeStr, &stampConfig->fontSize);
- err = ResError();
-
- // Restore the original resource file and exit.
-
- CouldNotLoadString:
-
- UseResFile(oldResFile);
- return err;
- }
-
-
- /*******************************************************************
- GetStamp returns a StampCollection item, if there's one set up.
-
- ********************************************************************/
-
- OSErr GetStamp(StampCollection *stampConfig)
- {
- return GetJobCollectionItem(stampConfig, nil, kStampCollectionType, gxPrintingTagID);
- }
-